package com.android.nurse;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Result extends CommonActivity {
	private TextView tvUserName;
	private TextView tvTypeCategory;
	private ImageView imgTypeNurse;
	private TextView tvTiredPoint;
	private Button btnGoExplain;
	private String typeNurse;
	private int tiredPoint;
	private int idNurse;
	private String userName;
	private NurseType nurse;
	private int age;
	private LocalRankingInfo localRankingInfo;
	private LocalRanking lR;
	private int width;

	private Thread mThread = new Thread(new Runnable() {

		@Override
		public void run() {
			Long startTime = System.currentTimeMillis();
			// TODO Auto-generated method stub
			lR.postResultToServer(getPostURL(), getShowRanking());
			lR.tryToResend(getPostURL(), getShowRanking());
			lR.getListLocalRankToShow(getShowRanking(), getGetURL());
			Long stopTime = System.currentTimeMillis();
			if ((stopTime - startTime) >= 3000) {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						btnGoExplain.setVisibility(View.VISIBLE);
					}
				});
			} else {

				synchronized (this) {
					try {
						wait(3000 - (stopTime - startTime));
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								btnGoExplain.setVisibility(View.VISIBLE);
							}
						});
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Display display = ((WindowManager) this
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		width = display.getWidth();
		if (width > 500) {
			setContentView(R.layout.result_big);
		} else {
			setContentView(R.layout.result);
		}
		getData();
		// thread to update result to server and get global pos to update
		lR = new LocalRanking(localRankingInfo, getApplicationContext());

		mThread.start();
		
		LinearLayout lo = (LinearLayout) findViewById(R.id.layoutMain);
		tvUserName = (TextView) findViewById(R.id.tvUserName);
		tvTypeCategory = (TextView) findViewById(R.id.tvTypeCategory);
		Animation a = AnimationUtils.loadAnimation(this, R.anim.scalerotate);
		a.reset();
		lo.startAnimation(a);
		imgTypeNurse = (ImageView) findViewById(R.id.imgTypeNurse);
		tvTiredPoint = (TextView) findViewById(R.id.tvTiredPoint);
		btnGoExplain = (Button) findViewById(R.id.btnExplain);
		btnGoExplain.setVisibility(View.INVISIBLE);
		btnGoExplain.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_DOWN:
					if (width > 500) {
						btnGoExplain
								.setBackgroundResource(R.drawable.btn_explanation_big_2);
					} else {
						btnGoExplain
								.setBackgroundResource(R.drawable.btn_explanation_2);
					}
					break;
				case MotionEvent.ACTION_UP:
					if (width > 500) {
						btnGoExplain
								.setBackgroundResource(R.drawable.btn_explanation_big);
					} else {
						btnGoExplain
								.setBackgroundResource(R.drawable.btn_explanation);
					}
					Intent i = new Intent(getApplicationContext(),
							ExplainedScreen.class);
					i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
					customeActivity(i);

					break;
				}
				return true;
			}
		});
		nurse = getNurseType(idNurse);
		tvUserName.setText(Html.fromHtml("<font color='#fd6396'>" + userName
				+ "</font>さんのタイプは"));
		tvTypeCategory.setText(typeNurse + "ナース");
		imgTypeNurse.setImageResource(getResources().getIdentifier(
				nurse.getImage(), "drawable", getPackageName()));
		tvTiredPoint.setText(Html
				.fromHtml("<small>お疲れ度</small><font color='#fd6396'> "
						+ tiredPoint + "</font><small>%</small>"));

	}

	public void getData() {
		SharedPreferences settings = getSharedPreferences(
				QuestionsScreen.PREFS_NAME, 0);
		userName = settings.getString(LogInScreen.userName, "no exits!");
		typeNurse = settings.getString(QuestionsScreen.TYPE_NURSE, "nullll");
		idNurse = settings.getInt(QuestionsScreen.ID_NURSE, 10);
		tiredPoint = settings.getInt(QuestionsScreen.TIRED_POINT, 1000);
		age = settings.getInt(LogInScreen.ageNurse, -1);
		Log.d("user", userName);
		localRankingInfo = new LocalRankingInfo();
		localRankingInfo.setUsername(userName);
		localRankingInfo.setNurseType(idNurse);
		localRankingInfo.setAge(age);
		localRankingInfo.setTiredScore(tiredPoint);

	}

	@Override
	public void onBackPressed() {
		// mtds.te
		// mdmsds.syds
		return;
	}
}
